package cn.zcscloud.mall.utils;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description: 921SpringMVC
 * Copyright: Copyright (c) 2020
 * Company: 英才汇硕信息技术有限公司
 *
 * @author caoning <br>
 * @version 1.0 <br>
 * @created 2020/9/29
 */
public class SpecialDateEditor extends PropertyEditorSupport {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss.SSS");
        Date date = null;
        try {
            //防止空数据出错
            if (text != null && !text.equals("")) {
                date = format.parse(text);
            }
        } catch (ParseException e) {
            format = new SimpleDateFormat("yyyy-MM-dd HH");
            try {
                date = format.parse(text);
            } catch (ParseException e1) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                try {
                    date = format.parse(text);
                } catch (ParseException e2) {
                    format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        date = format.parse(text);
                    } catch (ParseException e3) {
                        format = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            date = format.parse(text);
                        } catch (ParseException e4) {
                            format = new SimpleDateFormat("yyyy-MM");
                            try {
                                date = format.parse(text);
                            } catch (Exception e5) {
                                logger.error("自动绑定日期数据出错", e);
                            }
                        }
                    }

                }
            }
        }
        setValue(date);
    }

}

